From cc0364c6e0012dbc5477795eec2aa2d11bc57080 Mon Sep 17 00:00:00 2001 From: tsteven4 <13596209+tsteven4@users.noreply.github.com> Date: Sun, 24 Oct 2021 18:15:15 -0600 Subject: [PATCH] replace the last uses of QRegExp. (#743) also, correct the discard documentation. The given example didn't work in recent releases. --- discard.cc | 64 +++++++++++++------- discard.h | 21 ++++--- trackfilter.cc | 11 ++-- trackfilter.h | 15 ++--- xmldoc/filters/options/discard-matchname.xml | 2 +- 5 files changed, 70 insertions(+), 43 deletions(-) diff --git a/discard.cc b/discard.cc index a052e3663..d0c1e8ca1 100644 --- a/discard.cc +++ b/discard.cc @@ -19,13 +19,16 @@ */ -#include "defs.h" #include "discard.h" -#include -// Can't use QRegularExpression because Linux won't get Qt 5 for years. -#include -#include -#include + +#include // for QDebug +#include // for QRegularExpression, QRegularExpression::CaseInsensitiveOption, QRegularExpressionMatch + +#include // for atoi, atof + +#include "defs.h" // for Waypoint, fatal, route_del_wpt, route_disp_all, track_del_wpt, track_disp_all, waypt_del, waypt_disp_all, route_head, rtedata, trkdata, wptdata, fix_none, fix_unknown +#include "src/core/logging.h" // for FatalMsg + #if FILTERS_ENABLED @@ -73,16 +76,16 @@ void DiscardFilter::fix_process_wpt(const Waypoint* wpt) del = 1; } - if (nameopt && name_regex.indexIn(waypointp->shortname) >= 0) { + if (nameopt && name_regex.match(waypointp->shortname).hasMatch()) { del = 1; } - if (descopt && desc_regex.indexIn(waypointp->description) >= 0) { + if (descopt && desc_regex.match(waypointp->description).hasMatch()) { del = 1; } - if (cmtopt && cmt_regex.indexIn(waypointp->notes) >= 0) { + if (cmtopt && cmt_regex.match(waypointp->notes).hasMatch()) { del = 1; } - if (iconopt && icon_regex.indexIn(waypointp->icon_descr) >= 0) { + if (iconopt && icon_regex.match(waypointp->icon_descr).hasMatch()) { del = 1; } @@ -130,6 +133,19 @@ void DiscardFilter::process() } +QRegularExpression DiscardFilter::generateRegExp(const QString& glob_pattern) +{ + QRegularExpression regex; + regex.setPatternOptions(QRegularExpression::CaseInsensitiveOption); + QString pattern = QRegularExpression::wildcardToRegularExpression(glob_pattern); + // un-anchor the pattern, we allow partial matches. + if (pattern.startsWith("\\A") && pattern.endsWith("\\z")) { + pattern = pattern.mid(2,pattern.size()-4); + } + regex.setPattern(pattern); + return regex; +} + void DiscardFilter::init() { if (hdopopt) { @@ -159,24 +175,28 @@ void DiscardFilter::init() } if (nameopt) { - name_regex.setCaseSensitivity(Qt::CaseInsensitive); - name_regex.setPatternSyntax(QRegExp::WildcardUnix); - name_regex.setPattern(nameopt); + name_regex = generateRegExp(nameopt); + if (!name_regex.isValid()) { + fatal(FatalMsg() << "discard: matchname option is an invalid expression."); + } } if (descopt) { - desc_regex.setCaseSensitivity(Qt::CaseInsensitive); - desc_regex.setPatternSyntax(QRegExp::WildcardUnix); - desc_regex.setPattern(descopt); + desc_regex = generateRegExp(descopt); + if (!desc_regex.isValid()) { + fatal(FatalMsg() << "discard: matchdesc option is an invalid expression."); + } } if (cmtopt) { - cmt_regex.setCaseSensitivity(Qt::CaseInsensitive); - cmt_regex.setPatternSyntax(QRegExp::WildcardUnix); - cmt_regex.setPattern(cmtopt); + cmt_regex = generateRegExp(cmtopt); + if (!cmt_regex.isValid()) { + fatal(FatalMsg() << "discard: matchcmt option is an invalid expression."); + } } if (iconopt) { - icon_regex.setCaseSensitivity(Qt::CaseInsensitive); - icon_regex.setPatternSyntax(QRegExp::WildcardUnix); - icon_regex.setPattern(iconopt); + icon_regex = generateRegExp(iconopt); + if (!icon_regex.isValid()) { + fatal(FatalMsg() << "discard: matchicon option is an invalid expression."); + } } } diff --git a/discard.h b/discard.h index 1dbb234f8..e27b41c51 100644 --- a/discard.h +++ b/discard.h @@ -22,12 +22,12 @@ #ifndef DISCARD_H_INCLUDED_ #define DISCARD_H_INCLUDED_ -// Can't use QRegularExpression because Linux won't get Qt 5 for years. -#include // for QRegExp -#include // for QVector +#include // for QRegularExpression +#include // for QString +#include // for QVector -#include "defs.h" // for ARG_NOMINMAX, ARGTYPE_BEGIN_REQ, ARGTYPE_S... -#include "filter.h" // for Filter +#include "defs.h" // for arglist_t, ARG_NOMINMAX, ARGTYPE_BEGIN_REQ, ARGTYPE_STRING, ARGTYPE_BOOL, ARGTYPE_INT, ARGTYPE_FLOAT, route_head, ARGTYPE_END_REQ, Waypoint, gpsdata_type +#include "filter.h" // for Filter #if FILTERS_ENABLED class DiscardFilter:public Filter @@ -40,6 +40,9 @@ public: void init() override; void process() override; +private: + QRegularExpression generateRegExp(const QString& glob_pattern); + private: char* hdopopt = nullptr; char* vdopopt = nullptr; @@ -50,13 +53,13 @@ private: char* eleminopt = nullptr; char* elemaxopt = nullptr; char* nameopt = nullptr; - QRegExp name_regex; + QRegularExpression name_regex; char* descopt = nullptr; - QRegExp desc_regex; + QRegularExpression desc_regex; char* cmtopt = nullptr; - QRegExp cmt_regex; + QRegularExpression cmt_regex; char* iconopt = nullptr; - QRegExp icon_regex; + QRegularExpression icon_regex; double hdopf{}; double vdopf{}; diff --git a/trackfilter.cc b/trackfilter.cc index 6d2ab94af..079bf36e7 100644 --- a/trackfilter.cc +++ b/trackfilter.cc @@ -36,11 +36,8 @@ #include // for QChar #include // for QDate #include // for QDateTime -#ifdef TRACKF_DBG #include -#endif #include // for QList<>::iterator, QList, QList<>::const_iterator -#include // for QRegExp, QRegExp::WildcardUnix #include // for QRegularExpression, QRegularExpression::CaseInsensitiveOption, QRegularExpression::PatternOptions #include // for QRegularExpressionMatch #include // for QString @@ -52,6 +49,7 @@ #include "grtcirc.h" // for RAD, gcdist, radtometers, heading_true_degrees #include "src/core/datetime.h" // for DateTime +#include "src/core/logging.h" // for FatalMsg #if FILTERS_ENABLED || MINIMAL_FILTERS @@ -187,7 +185,12 @@ void TrackFilter::trackfilter_fill_track_list_cb(const route_head* track) /* ca } if (opt_name != nullptr) { - if (!QRegExp(opt_name, Qt::CaseInsensitive, QRegExp::WildcardUnix).exactMatch(track->rte_name)) { + QRegularExpression regex(QRegularExpression::wildcardToRegularExpression(opt_name), + QRegularExpression::CaseInsensitiveOption); + if (!regex.isValid()) { + fatal(FatalMsg() << "track: name option is an invalid expression."); + } + if (!regex.match(track->rte_name).hasMatch()) { foreach (Waypoint* wpt, track->waypoint_list) { track_del_wpt(const_cast(track), wpt); delete wpt; diff --git a/trackfilter.h b/trackfilter.h index 721f02d9f..79eccb1a3 100644 --- a/trackfilter.h +++ b/trackfilter.h @@ -22,13 +22,14 @@ #ifndef TRACKFILTER_H_INCLUDED_ #define TRACKFILTER_H_INCLUDED_ -#include // for QDateTime -#include // for QList -#include // for QVector -#include // for qint64 - -#include "defs.h" // for ARG_NOMINMAX, route_head (ptr only), ARG... -#include "filter.h" // for Filter +#include // for QDateTime +#include // for QList +#include // for QVector +#include // for qint64 + +#include "defs.h" // for ARG_NOMINMAX, route_head (ptr only), ARG... +#include "filter.h" // for Filter +#include "src/core/datetime.h" // for DateTime #if FILTERS_ENABLED || MINIMAL_FILTERS diff --git a/xmldoc/filters/options/discard-matchname.xml b/xmldoc/filters/options/discard-matchname.xml index 1162329c0..5073cdf0c 100644 --- a/xmldoc/filters/options/discard-matchname.xml +++ b/xmldoc/filters/options/discard-matchname.xml @@ -10,7 +10,7 @@ by an alphanumeric sequence of variable length. To remove all six character long IDs that between (and including) GC1000 and GC2FFF, you could use -gpsbabel -i geo -f geocaching.loc -x discard,matchname=GC[1-2]... +gpsbabel -i geo -f geocaching.loc -x discard,matchname=GC[1-2]??? to discard all GCs followed by exactly three characters. -- 2.30.2